﻿/******************************************************************************
'Description:报表系统登录类
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:zhunan
'create Date:2017.06.22
 ********************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using uMES.LeanManufacturing.ParameterDTO;
using uMES.LeanManufacturing.Common;
using uMES.LeanManufacturing.DBUtility;

namespace uMES.LeanManufacturing.ReportBusiness
{
    public class uMESLoginBusiness
    {

        public bool Login(string strUid, string strEncryPwd, string password, out string strInfo)
        {
            ADHelper help = new ADHelper();
            string sql = string.Format("select t.password from employee t where lower(t.employeename)='{0}' ", strUid.ToLower());
            DataTable dt = OracleHelper.GetDataTable(sql);
            if (dt.Rows.Count == 0)
            {
                strInfo = "登录失败，未找到用户！";
                return false;
            }

            string pwd = dt.Rows[0]["password"].ToString();

            if ((pwd == strEncryPwd) || (pwd == password))
            {
                strInfo = "";
                return true;
            }
            else
            {
                strInfo = "登录失败，密码错误！";
                return false;
            }

        }

        // 登录成功时，根据用户名获取用户相关信息
        public DataTable getEmployeeInfo(string username)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(@"select t.employeeid,t.fullname,t.employeename,f.factoryid,f.factoryname,tm.teamname,tm.teamid 
                        from employee t
                        left join sessionvalues sv on sv.sessionvaluesid = t.sessionvaluesid
                        left join factory f on f.factoryid = sv.factoryid
                        left join team tm on tm.teamid = t.teamid
                        where lower(t.employeename)='" + username.ToLower() + "'");
            DataTable dtResult = OracleHelper.GetDataTable(sb.ToString());
            return dtResult;
        }


        public DataTable GetLoginInfo(string strUid)
        {
            string strSql = "select t.fullname,t.employeeid,t.employeename,r.roleid,r.rolename,f.factoryid,f.factoryname from employee t "; //,tt.teamid, tt.teamname
            strSql += "left join employeerole er on er.employeeid=t.employeeid ";
            strSql += "left join roledef r on r.roleid=er.roleid ";
            strSql += "left join sessionvalues se on se.sessionvaluesid=t.sessionvaluesid ";
            strSql += "left join factory f on f.factoryid = se.factoryid ";
            strSql += " where lower(t.employeename) = '" + strUid.ToLower() + "'";

            DataTable factoryTable = OracleHelper.GetDataTable(strSql);
            return factoryTable;
        }


        /// <summary>
        /// 查询登录人的角色 
        /// </summary>
        /// <returns></returns>
        public DataTable GetEmployeeRole(String strEmployeeid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("SELECT DISTINCT re.rolename,re.employeeid FROM roleemployee re");
            strSql.AppendLine("WHERE re.employeeid='" + strEmployeeid + "'");
            return OracleHelper.GetDataTable(strSql.ToString());
        }

    }
}
